﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Text;
using System.IO;

namespace ProjectDirector.Domain.Concretes
{
    public class DiagnosticLog
    {
        private static string errorPath = ConfigurationManager.AppSettings["ErrorPath"].ToString();
        private static string monitorPath = ConfigurationManager.AppSettings["MonitorPath"].ToString();

        public static void LogError(Exception ex)
        {
            StringBuilder sBuilder = new StringBuilder();
            sBuilder.AppendLine("Error Log - Date and Time of exception: " + DateTime.Now.ToString());
            sBuilder.AppendLine("Exception: " + ex.Message);            
            sBuilder.AppendLine("Stack Trace: " + ex.StackTrace);
            sBuilder.AppendLine("Source: " + ex.Source + Environment.NewLine);

            RecordError(sBuilder.ToString());
        }

        public static void LogModelError(string errorMessage)
        {
            StringBuilder sBuilder = new StringBuilder();
            sBuilder.AppendLine("Model Error Log - Date and Time of exception: " + DateTime.Now.ToString());
            sBuilder.AppendLine(errorMessage + Environment.NewLine);

            RecordError(sBuilder.ToString());
        }

        private static void RecordError(string error)
        {
            if (!string.IsNullOrEmpty(error))
            {
                if (File.Exists(errorPath))
                {
                    StreamWriter writer = new StreamWriter(errorPath, true);
                    writer.WriteLine(error);
                    writer.Close();
                }
            }
        }
    }
}